<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['dailyreportmaster'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['dailyreportmaster'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
  $currentCreateDate       = date("d-m-Y");
  $dailyReportMasterId     = "";
  $userName                = array();
  $userId                  = array();
  $createDate              = "";
  $reportDate              = "";
  $partyId                 = "";
  $outCome                 = "";
  $remarks                 = "";
  $nextFollowUp            = "";
  $typeOfConclusionId      = "";
  $typeOfConclusion        = "";
  $companyName             = "";
  $userTargetId            = "";
  $target                  = "";
  $achievedTarget          = "";
  $totalTarget             = "";
  $totalAchievedTarget     = "";
  $totalBalanceTarget      = "";
  $achievedTargetArray     = array();
  $dailyReportCount        = 0; 
  $userSelected            = 0; 
  if(isset($_POST['reportDateDay']))
  { 
    $createDate      = date("Y-m-d H:i:s");
    $reportDate      = $_POST['reportDateYear']."-".$_POST['reportDateMonth']."-".$_POST['reportDateDay']; 
    $marketFeedback  = isset($_POST['marketFeedback']) ? $_POST['marketFeedback'] : " ";
    $compInformation = isset($_POST['compInformation']) ? $_POST['compInformation'] : " ";
    
    $insertDailyReport = "INSERT INTO dailyreportmaster (userId, reportDate, createDate, marketFeedback, compInformation)
                               VALUES(".$_SESSION['s_activeUserId'.$cfgPack].",'".$reportDate."','".$createDate."','".$marketFeedback."','".$compInformation."')";
    $insertDailyReportResult = mysql_query($insertDailyReport);
    $dailyReportMasterId = mysql_insert_id();
    $loopCount = 0;
    $loopCountExtra = 0;
    while($loopCount < count($_POST['outCome']))
    {
      $partyId      = ($_POST['partyId'][$loopCount] != '') ? $_POST['partyId'][$loopCount] : 0;
      $outCome      = ($_POST['outCome'][$loopCount] != '') ? nl2br(addslashes($_POST['outCome'][$loopCount])) : "";
      $typeOfConclusionId  = ($_POST['typeOfConclusionId'][$loopCount] != '') ? $_POST['typeOfConclusionId'][$loopCount] : 0;
      if($typeOfConclusionId == 1)
      {
	      $remarks      = isset($_POST['remarks'][$loopCount]) ? nl2br(addslashes($_POST['remarks'][$loopCount])) : "";
	      $nextFollowUp = isset($_POST['nextFollowUp'][$loopCount*3+2]) ? 
	                      $_POST['nextFollowUp'][$loopCount*3+2]['Year']."-".$_POST['nextFollowUp'][$loopCount*3+1]['Month']."-".$_POST['nextFollowUp'][$loopCount*3]['Day']: '0000-00-00';
      }
      else
      {
      	$remarks = "";
      	$nextFollowUp = '0000-00-00';
      }
      if($_POST['outCome'][$loopCount] != "" || $_POST['typeOfConclusionId'][$loopCount] != "" )
      {
        $insertDeliveryChallanDetail = "INSERT INTO dailyreportdetail (dailyReportMasterId,partyId,
                                                                       outCome,remarks,nextFollowUp,typeOfConclusionId)
                                             VALUES('".$dailyReportMasterId."','".$partyId."',
                                                    '".$outCome."','".$remarks."',
                                                    '".$nextFollowUp."','".$typeOfConclusionId."')";
        $insertDeliveryChallanDetailResult = mysql_query($insertDeliveryChallanDetail);
        if(!$insertDeliveryChallanDetailResult)
          die("Insert Query Not Inserted : ".mysql_error());
      } 
      $loopCount++;
    }
    header("Location:dailyReportList.php");    
  }
  ///// Select user : Start
  $selectUser = "SELECT userId,userName
                   FROM user
               ORDER BY userName";
  $selectUserResult = mysql_query($selectUser);
  $a = 0;
  while($userRow = mysql_fetch_array($selectUserResult))
  { 
    $userId[$a]   = $userRow['userId'];
    $userName[$a] = $userRow['userName'];
    $a++;
  }
  ///// Select party : Start
  $selectParty = "SELECT partyId,companyName
                   FROM party
               ORDER BY companyName";
  $selectPartyResult = mysql_query($selectParty);
  $b = 0;
  while($partyRow = mysql_fetch_array($selectPartyResult))
  { 
    $partyId[$b]     = $partyRow['partyId'];
    $companyName[$b] = substr_replace($partyRow['companyName'],'...',35);
    $b++;               
  }
  
   ///// Select typeOfConclusion : Start                                 
  $selectConclusions = "SELECT typeOfConclusionId,typeOfConclusion                 
                          FROM typeofconclusion";                              
  $selectConclusionsResult = mysql_query($selectConclusions);            
  $c = 0;                                                    
  while($conclusionsRow = mysql_fetch_array($selectConclusionsResult))   
  {                                                          
    $typeOfConclusionId[$c]  = $conclusionsRow['typeOfConclusionId'];                 
    $typeOfConclusion[$c]    = $conclusionsRow['typeOfConclusion'];             
    $c++;                                                    
  }           
  
  $selectAchievedTarget = "SELECT userTargetId,target,achievedTarget
	                           FROM usertarget
	                          WHERE userId =".$_SESSION['s_activeUserId'.$cfgPack]." 
	                            AND month ='".date('Y-m-01')."'";
	$selectAchievedTargetRes = mysql_query($selectAchievedTarget);
	$achievedTargetRow = mysql_fetch_array($selectAchievedTargetRes);
	$target         = $achievedTargetRow['target'];
	$achievedTarget = $achievedTargetRow['achievedTarget'];
	$totalTarget         += $achievedTargetRow['target'];
  $totalAchievedTarget += $achievedTargetRow['achievedTarget'];
  $totalBalanceTarget  += $achievedTargetRow['target'] - $achievedTargetRow['achievedTarget'];
  
  include("./bottom.php");
  $smarty->assign("currentCreateDate",$currentCreateDate);
  $smarty->assign("userId",$userId);
  $smarty->assign("userName",$userName);
  $smarty->assign("userSelected",$userSelected);
  $smarty->assign("reportDate",$reportDate);
  $smarty->assign("createDate",$createDate);
  $smarty->assign("dailyReportMasterId",$dailyReportMasterId);
  $smarty->assign("partyId",$partyId);
  $smarty->assign("companyName",$companyName);
  $smarty->assign("outCome",$outCome);
  $smarty->assign("remarks",$remarks);
  $smarty->assign("nextFollowUp",$nextFollowUp);
  $smarty->assign("typeOfConclusionId",$typeOfConclusionId);
  $smarty->assign("typeOfConclusion",$typeOfConclusion);
  $smarty->assign("userTargetId",$userTargetId);
  $smarty->assign("target",$target);
  $smarty->assign("achievedTarget",$achievedTarget);
  $smarty->assign("achievedTargetArray",$achievedTargetArray);
  $smarty->assign("totalTarget",$totalTarget);
  $smarty->assign("totalAchievedTarget",$totalAchievedTarget);
  $smarty->assign("totalBalanceTarget",$totalBalanceTarget);
  $smarty->display("dailyReport.tpl");
}
?>